﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Text;
using System.Web.Mvc.Ajax;

namespace KCMS.Helpers
{
    public static class Paging
    {
        public static MvcHtmlString PagingNavigator(this AjaxHelper helper,int currentPage,int itemCounts,int pageSize,Guid categoryId,int filter,Guid brendId)
        {
            StringBuilder sb = new StringBuilder();
            AjaxOptions ao = new AjaxOptions();
            ao.UpdateTargetId = "product-list-container";
            sb.Append("<span>Страницы </span>");
            if (currentPage != 0)
            {
                sb.Append(helper.ActionLink("<", "ShowListByCategory", new { categoryId = categoryId, pageNum = currentPage - 1, filter = filter, brendId = brendId }, ao, new { @class = "paging" }));
            }
            else
            {
                //sb.Append(HttpUtility.HtmlEncode("<span><</span>"));
                sb.Append("<span><</span>");
            }
            sb.Append(" ");
              double res=Math.Ceiling((double)itemCounts / pageSize);
            int pagesCount = (int)res;
            for (int i = 0; i < pagesCount; i++)
            {
                if (i != currentPage)
                {
                    sb.Append(helper.ActionLink((i + 1).ToString(), "ShowListByCategory", new { categoryId = categoryId, pageNum = i, filter = filter, brendId = brendId }, ao, new { @class = "paging" }));
                }
                else {
                    sb.Append("<span>"+(i+1).ToString()+"</span>");
                }
                sb.Append(" ");
            }
            if (currentPage < pagesCount - 1)
            {
                sb.Append(helper.ActionLink(">", "ShowListByCategory", new { categoryId = categoryId, pageNum = currentPage + 1, filter = filter, brendId = brendId }, ao, new { @class = "paging" }));

            }
            else
            {
                sb.Append("<span>></span>");
            }
            return MvcHtmlString.Create(sb.ToString());
        }
    }
}